package com.cqie.tes.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqie.tes.comm.R;
import com.cqie.tes.comm.qo.BaseQo;
import com.cqie.tes.comm.service.impl.BaseServiceImpl;
import com.cqie.tes.domain.Student;
import com.cqie.tes.domain.qo.tes.StudentQo;
import com.cqie.tes.domain.vo.tes.student.StudentVo;
import com.cqie.tes.mapper.StudentMapper;
import com.cqie.tes.mapper.SysUserMapper;
import com.cqie.tes.service.ClazzService;
import com.cqie.tes.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DD-GKD
 * @since 2020-09-18
 */
@Service
public class StudentServiceImpl extends BaseServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzService clazzService;
    // TODO 修改为service
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public R getList(BaseQo query) {
        // 1
        StudentQo qo = (StudentQo) query;
        // 2
        QueryWrapper<Student> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(qo.getName())){
            qw.like("name",qo.getName());
        }
        qw.eq("mark", 1);
        qw.orderByDesc("id");
        // 3
        IPage<Student> page = new Page<>(qo.getPage(),qo.getLimit());
        IPage<Student> data = studentMapper.selectPage(page,qw);
        List<Student> roleList = data.getRecords();

        // 4
        List<StudentVo> studentVoList = new ArrayList<>();

        if (!roleList.isEmpty()) {
            roleList.forEach(item->{
                StudentVo studentVo = new StudentVo();
                BeanUtils.copyProperties(item,studentVo);
                // 设置vo字段
                if (studentVo.getId() != null){
                    studentVo.setClazzName(clazzService.getById(studentVo.getClazzId()).getName());
                }

                if (studentVo.getUserId() != null){
                    studentVo.setUserAccount(sysUserMapper.selectById(studentVo.getUserId()).getUsername());
                }
                studentVoList.add(studentVo);
                System.out.println(studentVo);
            });
        }
        Long total = data.getTotal();
//        System.out.println(total);

        return R.ok().message("查询成功").data("total", total).data("rows", studentVoList);
    }


    @Override
    public R edit(Student entity) {
        return super.edit(entity);
    }

    @Override
    public R deleteById(Integer id) {
        if (id == null || id == 0) {
            return R.error().message("记录ID不能为空");
        }
        Student entity = this.getById(id);
        if (entity == null) {
            return R.error().message("记录不存在");
        }
        return super.delete(entity);
    }



    @Override
    public R optionList() {
        return null;
    }
}
